<?php
	session_start();
	
	if (!isset($_SESSION['sesi_login']))
		header("Location:../../index.php");
	
	@chdir("../../");
	define('ABS_URL',str_replace("\\","/",getcwd()));
	require ABS_URL.'/engine/Smarty.class.php';
	require ABS_URL."/functions/class.query.inc.php";
	$smarty = new Smarty();
	require ABS_URL."/dev_con.inc.php";
	require ABS_URL."/functions/func_file.php";
	$queryData = new queryData();
	$queryData->baseConfig();
	require ABS_URL."/functions/func_content.php";
	require ABS_URL."/langs/".BASE_LANGUAGE.'.inc.php';
	require $queryData->get_curr_dir(dirname(__FILE__))."/langs/".BASE_LANGUAGE.'.inc.php';

	$nama = trim(strip_tags($_POST['nama']));
	$url = strip_tags($_POST['deskripsi']);
	$author = strtolower($_SESSION['author']);
	$upbanner = $_FILES['upbanner']['name'];
	$size = $_FILES['upbanner']['size'];
	$tanggal = date("Y-m-d-H-i-s");
	$thum = $_POST['nama_hid'];

	function getFileExtension($str) {
			$i = strrpos($str,".");
			if (!$i) { return ""; }
			$l = strlen($str) - $i;
			$ext = substr($str,$i+1,$l);
			return $ext;
	}

	$aext = getFileExtension($upbanner);
	$aext = strtolower($aext);

	if (strtolower(ACT_VER)=="hapus") {
		$kode = (int) $_GET['kode'];
		
		$sqls = "SELECT * FROM tbl_upload WHERE idx='".$kode."' LIMIT 1";
		$qrys = mysql_query($sqls);
		$rows = mysql_fetch_object($qrys);
		$nm = explode("[spasi]",$rows->data);
		$dataku = "../../data/galeri/FG".$nm[0].".".$rows->tipe;
								
		if (file_exists($dataku)) {	
			unlink($dataku);
		};

		$sql = "DELETE FROM tbl_upload WHERE idx='".$kode."'";
		$qry = mysql_query($sql);

		($qry) ? $_SESSION['sesi_ver'] = $queryData->cekAksi($kode,'delete') : $_SESSION['sesi_ver'] = '';
		$act = 'lihat';
	} else {		
		$a = array($nama,$upbanner,$aext);
		$b = array('empty','fileDataSize','fileGaleri');
		$c = array($FG_TITLE,$FG_FAILED_SIZE,$FG_FAILED_EXTENSION);
		switch (strtolower($_POST['act_ver'])) {
			case "input" :
				$result = $queryData->NotificationCheck($a,$b,$c);
				if (empty($result)) {						
					$dataku = "../../data/galeri/FG".date("dMYHis").".".$aext;
					
					if (move_uploaded_file($_FILES['upbanner']['tmp_name'],$dataku)) {
						$sql_upload = "INSERT INTO tbl_upload VALUES(NULL,'".date("dMYHis")."[spasi]".$nama."[spasi]".$ukuran."[spasi]".$url."[spasi]FG','".$aext."','".$tanggal."','".$author."','".$tanggal_mulai."','".$tanggal_selesai."','".$hari."','".$jam_mulai."','".$jam_selesai."')";
						$qry_upload = mysql_query($sql_upload);
						$_SESSION['sesi_ver'] = $queryData->cekAksi($nama,'add');
						$act = 'lihat';
					} else {
						$_SESSION['sesi_ver'] = $FG_FAILED_UPLOAD;	
					}
				} else {
					$_SESSION['sesi_ver'] = $DEV_ACTION_NOTIFICATION;
					foreach($result as $data) {
						$_SESSION['sesi_ver'] .= '<br />- '.$data;
					}
					$act = 'tambah';
				}
			break;
			case "edit" :
				$kode = (int) $_POST['kode'];
				$result = $queryData->NotificationCheck($a,$b,$c);
				if (!empty($upbanner)) {
					if (empty($result)) {						
						$dataku = "../../data/galeri/FG".date("dMYHis").".".$aext;
						if (move_uploaded_file($_FILES['upbanner']['tmp_name'],$dataku)) {
							$sqls = "SELECT * FROM tbl_upload WHERE idx='".$kode."' LIMIT 1";
							$qrys = mysql_query($sqls);
							$rows = mysql_fetch_object($qrys);
							$nm = explode("[spasi]",$rows->data);
							$dataku = "../../data/galeri/FG".$nm[0].".".$rows->tipe;
		
							if (file_exists($dataku)) {	
								unlink($dataku);
							};
							
							$sql_img = "DELETE FROM tbl_upload WHERE idx='".$kode."'";
							$qry_img = mysql_query($sql_img);
							
							$sql_upload = "INSERT INTO tbl_upload VALUES(NULL,'".date("dMYHis")."[spasi]".$nama."[spasi]".$ukuran."[spasi]".$url."[spasi]FG','".$aext."','".$tanggal."','".$author."','".$tanggal_mulai."','".$tanggal_selesai."','".$hari."','".$jam_mulai."','".$jam_selesai."')";
							$qry_upload = mysql_query($sql_upload);
							
							$_SESSION['sesi_ver'] = $queryData->cekAksi($nama,'edit');
							$act = 'lihat';
						} else {
							$_SESSION['sesi_ver'] = $FG_FAILED_UPLOAD;
						}
					} else {
						$_SESSION['sesi_ver'] = $DEV_ACTION_NOTIFICATION;
						foreach($result as $data) {
							$_SESSION['sesi_ver'] .= '<br />- '.$data;
						}
						$act = 'tambah';
					}
				} else {
					$a = array($nama);
					$b = array('empty');
					$c = array($FG_TITLE);
					$result = $queryData->NotificationCheck($a,$b,$c);
					if (empty($result)) {						
						$sql_upload = "UPDATE tbl_upload SET data='".$thum."[spasi]".$nama."[spasi]".$ukuran."[spasi]".$url."[spasi]FG', tanggal='".$tanggal."' WHERE idx='".$kode."'";
						$qry_upload = mysql_query($sql_upload);
						$_SESSION['sesi_ver'] = $queryData->cekAksi($nama,'edit');
						$act = 'lihat';
					} else {
						$_SESSION['sesi_ver'] = $DEV_ACTION_NOTIFICATION;
						foreach($result as $data) {
							$_SESSION['sesi_ver'] .= '<br />- '.$data;
						}
						$act = 'tambah';
					}
				}
			break;
		}
	}
	
	if (isset($_SESSION['sesi_login'])) {
		header('Location:../../?show='.SHOW.'&showview='.SHOWVIEW.'&act='.$act.'&limitdown=0');
	} else {
		header("Location:../../index.php");
	};
?>
